﻿using NPOI.SS.UserModel;
using System;
using System.ComponentModel;
using System.IO;
using System.Threading;

namespace XLSranker
{
    class CompareRow
    {
        public static void compareRow(IWorkbook workbook, BackgroundWorker worker)
        {
            int n = 1;//计数变量

            ISheet sheet = workbook.GetSheetAt(0);
            
            int length = sheet.GetRow(0).GetCell(0).ToString().Length;
            int totalNoNullNum = 0;           

            for (int a = 0; a <= sheet.LastRowNum; a++)
            {
                if (sheet.GetRow(a).GetCell(0) != null)
                {
                    if (sheet.GetRow(a).GetCell(0).ToString() != "")
                        totalNoNullNum++;
                }
            }

            //Console.WriteLine("incom,sheet.LastRowNum={0}", sheet.LastRowNum);
            //Console.WriteLine("incom,totalNoNullNum={0}", totalNoNullNum);


            for (int i = 1; i <= totalNoNullNum - 1; i++)
            {
                //Console.WriteLine(i);
                if (i < totalNoNullNum - 1)
                {
                    int num1 = int.Parse(sheet.GetRow(i - 1).GetCell(0).ToString().Substring(length - 8, 8)); //获取上一行的串号后8位
                    int num2 = int.Parse(sheet.GetRow(i).GetCell(0).ToString().Substring(length - 8, 8));  //获取当前行的串号后8位

                    if (num2 - num1 == 1)
                    {
                        n++;
                    }

                    else
                    {
                        FillCell.fillCell(sheet, i - 1, i - n, n);
                        n = 1;
                    }
                }
                else //此段代码用来处理最后一行数据
                {
                    int num1 = int.Parse(sheet.GetRow(i - 1).GetCell(0).ToString().Substring(length - 8, 8));
                    int num2 = int.Parse(sheet.GetRow(i).GetCell(0).ToString().Substring(length - 8, 8));

                    if (num2 - num1 == 1)
                    {
                        n++;
                        FillCell.fillCell(sheet, i, i - (n - 1), n);
                    }
                    else
                    {
                        FillCell.fillCell(sheet, i - 1, i - n, n);
                        FillCell.fillCell(sheet, i, i, 1);
                    }
                                       
                }
                float fl = ((float)i / (totalNoNullNum - 1));
                //Thread.Sleep(1);
                worker.ReportProgress((int)(fl * 100));
            }
            sheet.SetColumnWidth(0, 25 * 256);
            sheet.SetColumnWidth(1, 25 * 256);
            //FileStream fs2 = File.Create(@"C:\Users\GaoGuohua\Desktop\CRResult.xls");
            //workbook.Write(fs2);
            //fs2.Close();
        }
    }
}
